/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Net.Mail;
using System.Windows.Forms;
using SurveillanceDotNet.Model;

namespace SurveillanceDotNet.AlarmActions
{
    public partial class PlaySoundAlarmActionForm : Form
    {
        public PlaySoundAlarmAction Action { get; set; }

        private Database _Database;

        public PlaySoundAlarmActionForm()
        {
            InitializeComponent();

            _Database = Database.Instance;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tbxName.Text.Length == 0)
            {
                MessageBox.Show("Please enter a name for the action!", "Missing name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (tbxSoundFilePath.Text.Length == 0)
            {
                MessageBox.Show("Please select a sound file!", "Missing sound file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (Action == null)
            {
                Action = new PlaySoundAlarmAction();
            }

            if (chxLoop.Checked)
            {
                double loopTime;

                if (!double.TryParse(tbxLoopTime.Text, out loopTime))
                {
                    MessageBox.Show("Please enter a valid loop period!", "Invalid loop period", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Action.LoopFileTime = loopTime * 60d * 1000d;
            }

            Action.Name = tbxName.Text;
            Action.Code = tbxCode.Text;
            Action.SoundFilePath = tbxSoundFilePath.Text;
            Action.LoopFile = chxLoop.Checked;

            this.DialogResult = DialogResult.OK;
        }

        private void PlaySoundAlarmActionForm_Load(object sender, EventArgs e)
        {
            tbxLoopTime.Visible = false;
            lblLoopTime.Visible = false;

            if (Action != null)
            {
                tbxName.Text = Action.Name;
                tbxCode.Text = Action.Code;
                tbxSoundFilePath.Text = Action.SoundFilePath;
                chxLoop.Checked = Action.LoopFile;
                tbxLoopTime.Text = (Action.LoopFileTime / 60d / 1000d).ToString();
            }
            else
            {
                int exitingActionCounter = 1;

                foreach (AlarmAction action in _Database.AlarmActions)
                {
                    if (action is PlaySoundAlarmAction) exitingActionCounter++;
                }

                tbxName.Text = "Play Sound " + exitingActionCounter;
            }
        }

        private void chxLoop_CheckedChanged(object sender, EventArgs e)
        {
            lblLoopTime.Visible = chxLoop.Checked;
            tbxLoopTime.Visible = chxLoop.Checked;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (ofdSoundFile.ShowDialog() == DialogResult.OK)
            {
                tbxSoundFilePath.Text = ofdSoundFile.FileName;
            }
        }
    }
}
